#! /usr/bin/perl 

$reads = $ARGV[0];
$directory = $ARGV[1];
$min_contig = 150;
$local_maxk = 120;
$long_read_file = $directory . "/long_reads.fa";
$mink = 40;
$maxk = 100;
$step = 10;
$min_similar = 0.95;
$max_similar = 0.95;

$cmd = "mkdir $directory";
print $cmd, "\n";
system ($cmd);

$cmd = "cat /dev/null > $long_read_file";
print $cmd, "\n";
system ($cmd);

#$cmd = "bin/idba -r $reads -o $directory --mink $mink --maxk $mink";
#print $cmd, "\n";
#system ($cmd);
#
#$cmd = "cat $directory/contig.fa $directory/contig.fa > $long_read_file";
#print $cmd, "\n";
#system ($cmd);
#
#$nextk = $mink + $step;
#$cmd = "mkdir $directory/$nextk ";
#print $cmd, "\n";
#system ($cmd);
#
#$cmd = "cat $directory/contig.fa > $directory/$nextk/graph.fa";
#print $cmd, "\n";
#system ($cmd);

for ($kmer = $mink; $kmer <= $maxk; $kmer += $step)
{
	$old_kmer = $kmer - $step;
	if ($kmer == $mink)
	{
		$cmd = "bin/idba -r $reads -l $long_read_file -o $directory/$kmer --mink $kmer --maxk $kmer";
	}
	else
	{
		$cmd = "bin/iterate -r $reads -l $long_read_file -o $directory/$kmer --mink $old_kmer --maxk $kmer";
	}

	print $cmd, "\n";
	system ($cmd);

	$cmd = "bin/filter_sequences $directory/$kmer/contig.fa $directory/$kmer/contig-filtered.fa --min_contig $min_contig";
	print $cmd, "\n";
	system ($cmd);

	$similar = $kmer * 1.0 / 100;
	$similar = ($similar > $min_similar) ? $similar : $min_similar;
	$similar = ($similar < $max_similar) ? $similar : $max_similar;
#	$similar = 0.95;

	$cmd = "bin/align $directory/$kmer/contig-filtered.fa $reads $directory/$kmer/align --similar $similar";
	print $cmd, "\n";
	system ($cmd);

	$cmd = "bin/local_assembly -r $reads -o $directory/$kmer";
	print $cmd, "\n";
	system ($cmd);

	$cmd = "cat $directory/$kmer/local-contig.fa $directory/$kmer/local-contig.fa > $long_read_file";
	print $cmd, "\n";
	system ($cmd);

#	$cmd = "cat $directory/$kmer/contig.fa $directory/$kmer/contig.fa >> $long_read_file";
#	print $cmd, "\n";
#	system ($cmd);

	$nextk = $kmer + $step;
	$cmd = "mkdir $directory/$nextk ";
	print $cmd, "\n";
	system ($cmd);

	$cmd = "cat $directory/$kmer/contig.fa > $directory/$nextk/graph.fa";
	print $cmd, "\n";
	system ($cmd);
}

#$cmd = "bin/idba -l $long_read_file -o $directory/final --min_count 0 --min_cover 0 --min_support 1 --mink $maxk --maxk $local_maxk";
#print $cmd, "\n";
#system ($cmd);

$cmd = "bin/scaffold -r $reads  -o $directory/$maxk ";
print $cmd, "\n";
system ($cmd);

$cmd = "bin/local_scaffold_new -r $reads  -o $directory/$maxk ";
print $cmd, "\n";
system ($cmd);

